/**
 * Created by Been on 2017-09-14.
 */

var VM = new Vue({
    el:"#deptApp",
    created:function(){
        this.lay();
        this.getMsgType();
    },
    data:{
        totalNum:"",//总条数
        msgType:[],//消息类型 单选
        deptLists:[],//动态表单
        delMultiFlag:[],
        allChoseFlag:false,
        keyWord:'',
        pageChange: {
            current: 1,//当前页
            pagesize: 5,//每页5行
            total:"",
            pageNumber:""
        },
    },
    methods:{
        lay:function(){
            var keyWord = $("#leave_type").attr("selectedCode");
            var _this = this;
            layui.use(['laydate','element','laypage','layer','form'], function(){

                var  laypage = layui.laypage,//分页
                    layer = layui.layer;//弹出层

                // 以上模块根据需要引入
                getPageData(_this.pageChange.current);
                /*请求后台数据，分页请求*/

                function getPageData(curr) {

                    $.ajax({
                        url:"../../../actmodel/list.do?current="+curr+"&pagesize="+_this.pageChange.pagesize+"&category="+keyWord,
                        dataType:"json",
                        type:"POST",
                        async:false,
                        success:function (res) {
                            console.log(res);
                            if(res.msg=="success"){
                                var list = res.result.dataList;
                                _this.deptLists = list;

                                _this.pageChange.total = res.result.total;
                                _this.pageChange.pageNumber = Math.ceil( _this.pageChange.total/_this.pageChange.pagesize);

                            }else{
                                layer.msg("没有查询到数据！", {icon: 1})
                            }
                        },
                        error:function (XMLHttpRequest) {
                            layer.msg("系统错误", {icon: 1});
                        }
                    })
                }
            });
        },
        pageUpOrDown:function(type){
            if(isNaN(type)){
                if(type == "prev"){
                    if(this.pageChange.current > 1){
                        this.pageChange.current--;
                    }
                }
                if(type == "next"){
                    if(this.pageChange.current < this.pageChange.pageNumber){
                        this.pageChange.current++;
                    }
                }
            }else{
                this.pageChange.current = type;
            }
        },
        getMsgType:function(){
            var _this = this;
            $.ajax({
                url:"../../../actmodel/processType.do",
                dataType:"json",
                type:"POST",
                async:false,
                success:function (res) {
                    console.log(res);
                    if(res.msg=="success"){
                        _this.msgType = res.result.categoryList;

                    }else{
                        layer.msg("没有查询到数据！", {icon: 1})
                    }
                },
                error:function (XMLHttpRequest) {
                    layer.msg("系统错误", {icon: 1});
                }
            })
        },
        allChose:function(){
            var _this = this;
            this.allChoseFlag = !this.allChoseFlag;
            this.deptLists.forEach(function(item,index){
                if(typeof item.checked == "undefined"){
                    Vue.set(item,"checked",true);
                    _this.delMultiFlag.push(item.id);
                }else{
                    item.checked = _this.allChoseFlag;
                    _this.delMultiFlag = [];
                }
            });
        },
        selectSelf:function(item){
            if(typeof item.checked == "undefined"){
                Vue.set(item,"checked",true);
            }else{
                item.checked = !item.checked;
            }
        },
        delMulti:function(){
            var _this = this;
            this.notiveLists.forEach(function (item,index) {
                _this.delMultiFlag.forEach(function (value,i) {
                    if(item.id == value){
                        item.id = 0;
                    }
                });
            });
        },
        addModel:function(){
            admin_add("新建模型","../../../actmodel/addmodel.do",'800','600');
        },
        edit:function (item) {
            parent.parent.window.open ( "../../../act/process-editor/modeler.jsp?modelId="+item.id , "_blank" );
        },
        deploy:function (item) {
            layer.open({
                content: '确认要部署该模型吗?',
                btn: ['确认', '取消'],
                yes: function(index, layero) {
                    $.ajax({
                        url:"../../../actmodel/deploy.do?id="+item.id,
                        dataType:"json",
                        type:"POST",
                        async:false,
                        success:function (res) {
                            layer.msg(res.result.message, {icon: 1});
                            // window.location.href = "../../../actprocess/list.do?category=null&current=1&pagesize=5";
                        },
                        error:function (XMLHttpRequest,data) {
                            console.log(data)
                            layer.msg("系统错误", {icon: 1});
                        }
                    });
                 layer.close(index);//关闭确定弹出框
                    // parent.layer.close(pindex);//关闭新建窗口
//

                               },
                cancel: function() {
                           //右上角关闭回调

                       }
               });
        },
        export1:function (item) {
            parent.parent.window.open ( "../../../actmodel/export.do?id="+item.id , "_blank" );
        },
        del:function (item) {
            layer.open({
                content: '确认要删除该模型吗?',
                btn: ['确认', '取消'],
                yes: function(index, layero) {
                    $.ajax({
                        url:"../../../actmodel/delete.do?id="+item.id,
                        dataType:"json",
                        type:"POST",
                        async:false,
                        success:function (res) {
                            layer.msg(res.result.message, {icon: 1});
                            window.location.href ="/page/activiti/model/model.html";
                        },
                        error:function (XMLHttpRequest) {
                            layer.msg("系统错误", {icon: 1});
                        }
                    });
                    layer.close(index);//关闭确定弹出框
                    // parent.layer.close(pindex);//关闭新建窗口
//

                },
                cancel: function() {
                    //右上角关闭回调

                }
            });
        }
    },
    filters: {
        formatDate: function (input) {
            var d = new Date(input);
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
            var hour = d.getHours() <10 ? '0' + d.getHours() : '' + d.getHours();
            var minutes = d.getMinutes() <10 ? '0' + d.getMinutes() : '' + d.getMinutes();
            var seconds = d.getSeconds() <10 ? '0' + d.getSeconds() : '' + d.getSeconds();
            return  year+ '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;

        },
        typeFormat:function(type){
            if(type == 1){
                return "分类1";
            }else if(type == 2){
                return "分类2"
            }else{
                return type;
            }
        }
    }
});//vue 实力结束
